// pages/mineUpdate/mineUpdate.js
const app = getApp()
const api = require('../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showName: false,
    avatarUrl: '',
    gender: '',
    nickName: '',
    tempName: '',
    ownSign: '',
    showOwnSign: false,
    tempOwnSign: '',
    radio: '1',
    message: ""
  },
  onChange(event) {
    console.log(event)
    this.setData({
      radio: event.detail,
      gender:event.detail
    });
  },
  onClick(event) {
    const {
      name
    } = event.currentTarget.dataset;
    this.setData({
      radio: name,
    });
  },
  saveNickName() {
    if (this.data.tempName) {
      this.setData({
        nickName: this.data.tempName,
        showName: false
      })
    } else {
      this.setData({
        showName: false
      })
    }

  },
  nameUpdate() {
    this.setData({
      showName: true,

    })
    console.log(this.data.showName)
  },
  onClose() {
    this.setData({
      showName: false
    })
  },
  inputNameIng(event) {
    this.setData({
      tempName: event.detail
    })

  },
  /*----修改个性签名 ------*/
  ownSignUpdate() {
    this.setData({
      showOwnSign: true
    })
  },
  onClose1() {
    this.setData({
      showOwnSign: false
    })
  },
  inputOwnSignIng(event) {
    this.setData({
      tempOwnSign: event.detail
    })
  },
  saveOwnSign() {
    if (this.data.tempOwnSign) {
      this.setData({
        ownSign: this.data.tempOwnSign,
        showOwnSign: false
      })
    } else {
      this.setData({
        showOwnSign: false
      })
    }
  },
  /*--------------------- */
  /*------------修改头像---------------- */
  headImg() {
    wx.chooseImage({
      sourceType: ['album', 'camera'],
      count: 1,
      success: (res) => {
        let tempFilePaths = res.tempFilePaths[0];
        wx.cloud.uploadFile({
          cloudPath: 'goodsImage/' + Date.now() + ".png", // 文件名称 
          filePath: tempFilePaths,
          success:res=>{
            console.log(res)
            wx.cloud.getTempFileURL({
              fileList:[{
                maxAge:3600*360,
                fileID:res.fileID
              }],
              success:Resurl=>{
                console.log(Resurl)
                console.log(Resurl.fileList[0].tempFileURL)
                this.setData({
                  avatarUrl:Resurl.fileList[0].tempFileURL
                })
              }
            })
          },
          fail:res=>{
            console.log(res)
          }
        })


      }
    })
  },
  /*----------------------------------- */

  /*--------------完成修改----------------- */
  sendIt(){
    console.log(app.globalData.userInfo.id)
    console.log(this.data.gender)
    wx.request({
      url: api.updateUserInfo.url,
      method:api.updateUserInfo.method,
      data:{
        id:app.globalData.userInfo.id,
        gender:this.data.gender,
        nickName:this.data.nickName,
        avatarUrl:this.data.avatarUrl,
        pro:this.data.ownSign
      },
      success:res=>{
        console.log(res)
      }
    })
    wx.switchTab({
      url: '../../pages/mine/mine',
    })
  },
  /*--------------------------------------- */
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    console.log(options)
    this.setData({
      avatarUrl: options.avatarUrl,
      gender: options.gender,
      nickName: options.nickName,
      ownSign: options.ownSign
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})